package com.example.home.park;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.bean.HotdetailBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.home.tool.StaticActi.ip;
import static com.example.home.tool.StaticActi.mimeType;

public class HotdetailsActivity extends AppCompatActivity {

    private ImageView new_img;
    private WebView new_text;
    private Handler handler;
    private Gson gson;
    private OkHttpClient okHttpClient;
    private String listID;
    private Request request;
    private HotdetailBean hotdetailBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotdetails);

        Intent intent = getIntent();
        listID = intent.getStringExtra("listID");


        initview();

        getBeanData();
    }

    private void initview() {
        new_img = findViewById(R.id.new_img);
        new_text = findViewById(R.id.new_text);

        okHttpClient = new OkHttpClient();
        handler = new Handler();
        gson = new Gson();
    }

    private void getBeanData() {
        request = new Request.Builder().url("http://124.93.196.45:10001/prod-api/api/park/press/press/"+listID).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                hotdetailBean = gson.fromJson(json, HotdetailBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(HotdetailsActivity.this).load(ip + hotdetailBean.getData().getCover()).into(new_img);
                        new_img.setScaleType(ImageView.ScaleType.FIT_XY);
                        new_text.loadDataWithBaseURL(null,hotdetailBean.getData().getContent(),mimeType,"utf-8",null);
                    }
                });
            }
        });
    }

}